<?php

namespace App\Backend\Http\Controllers;

use App\Down;
use App\Earning;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class DownController extends Controller
{
    //收费下载列表
    public function downlist($type, Request $request)
    {
        $data = $request->all();
        $where = [
            'type' => $type,
        ];
        if (!empty($request->name)) {
            $where['user_id'] = User::where('name',$request->name)->value('id');
        }
        if (!empty($request->earningname)) {
            $where['earning_user_id'] = User::where('name', $request->earningname)->value('id');
        }
        if (!empty($request->data_id)) {
            $where['data_id'] = $request->data_id;
        }
        $data['type'] = $type;

        if (!empty($request->date)) {
            $date = explode(' - ', $request->date);
            $data['downs'] = Earning::where($where)->whereBetween('created_at',[$date[0],$date[1]])->orderBy('id', 'desc')->paginate(20);
        } else {
            $data['downs'] = Earning::where($where)->orderBy('id', 'desc')->paginate(20);
        }
        $data['redirecturl'] = $this->redirecturl($type);
        return view('backend::down.list', $data);
    }
    //VIP免费下载列表
    public function downviplist($type, Request $request)
    {
        $data = $request->all();
        $where = [
            'type' => $type,
        ];
        if (!empty($request->name)) {
            $where['user_id'] = User::where('name',$request->name)->value('id');
        }
        if (!empty($request->earningname)) {
            $where['earning_user_id'] = User::where('name', $request->earningname)->value('id');
        }
        if (!empty($request->data_id)) {
            $where['data_id'] = $request->data_id;
        }
        $data['type'] = $type;

        if (!empty($request->date)) {
            $date = explode(' - ', $request->date);
            $data['downs'] = Down::where($where)->whereBetween('created_at',[$date[0],$date[1]])->orderBy('id', 'desc')->paginate(20);
        } else {
            $data['downs'] = Down::where($where)->orderBy('id', 'desc')->paginate(20);
        }
        $data['redirecturl'] = $this->redirecturl($type);
        return view('backend::down.viplist', $data);
    }

    public function redirecturl($type)
    {
        switch ($type){
            case '1' :
                return '/modeInfo';
            case '2' :
                return '/ModeInfoSu';
            case '3' :
                return '/modeInfoConstr';
            case '4' :
                return '/modeInfoConcept';
        }
    }
}
